<?php
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(MOBILE_DIR_PATH."/http_common.php");
require_once(MOBILE_DIR_PATH."/calendar_handler.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");
require_once(TRANSACTIONS_DIR_PATH."/usermanager.php");


class AuthHandler {
	private $user_name;
	private $user_pwd;
	private $byName;
	
	private $err_handler;
	private $http_handler;
	
	/*
	 * Parameter
	 * $uid is used as the return value of the user UID.
	 */
	public function auth($method) {
		
		$b = UserManager::auth($this->user_name, $this->user_pwd, $this->byName);
		if ($b == FALSE) {
			if ($this->err_handler instanceof IErrorHandler)
				$this->err_handler->doError();
		} else {
			// Authorization done. Save the UID of the user.
			if ($method == HTTP::GET)
				$_GET['uid'] = $b;
			else if ($method == HTTP::POST)
				$_POST['uid'] = $b;
				
			if ($this->http_handler === NULL) {
				HttpResponse::Respond(200);
				return;
			}
				
			if ($this->http_handler instanceof IHttpHandler)
				$this->http_handler->httpDo();
		}
	}
	
	public function __construct($name, $pwd, $byName) {
		$this->user_name	= $name;
		$this->user_pwd		= $pwd;
		$this->byName		= $byName;
		$this->err_handler	= NULL;
		$this->http_handler	= NULL;
	}
	
	public function setErrHandler($err_handler) {
		$this->err_handler = $err_handler;
	}
	
	public function setHttpHandler($http_handler) {
		$this->http_handler = $http_handler;
	}
}

/*
 * Pattern of the HTTP request:
 * GET or POST parameters:
 * name:   user name
 * pwd:    user password
 * by:     name is sent as name (TRUE, if it's user name.)
 * t:      action type you request to perform
 *  
 *
 */
class HttpAuthHandler extends AuthHandler {	
	private $method;
	
	public function __construct($method) {
		if ($method == HTTP::GET) {
			parent::__construct($_GET['name'], $_GET['pwd'], $_GET['by']);
			$_GET['uid'] = '';
		 } else if ($method == HTTP::POST) {
			parent::__construct($_POST['name'], $_POST['pwd'], $_POST['by']);
			$_POST['uid'] = '';
		}
		
		$this->method = $method;
		$this->setErrHandler(new HttpAuthErrHandler());
	}
	
	public function auth() {
		parent::auth($this->method);
	}
}



class HttpAuthErrHandler implements IErrorHandler {
	public function doError() {
		$this->respond();
	}
	
	private function respond() {
		//header("HTTP/1.1 401 UNAUTHORIZED");
		HttpResponse::Respond(401);
	}
}

class HttpDispatcher implements IHttpHandler {
	private $http_method;
	private $http_listeners;
	private $http_err_handler;
	
	public function __construct($m) {
		$this->http_method = $m;
		$this->http_listeners = array();
		$this->http_err_handler = new HttpStandardErrorHandler();
	}
	
	public function httpDo() {
		$http_params = null;
		if ($this->http_method == HTTP::GET) {
			$http_params = $_GET;
		} else if ($this->http_method == HTTP::POST) {
			$http_params = $_POST;
		}
		
		if ($http_params != null) {
			$bPassed = TRUE;
			
			$msg = '';
			foreach ($this->http_listeners as $l) {
				$http_err = $l->onRequestReceived($http_params);
				if ($http_err->getErrorCode() != HttpError::HTTP_OK) {
					$this->http_err_handler->setError($http_err);
					$this->http_err_handler->doError();
					
					$bPassed = FALSE;
				} else {
					if ($l instanceof IHttpOutputable)
						$msg .= $l->output();
				}
			}
			
			if ($bPassed)
				HttpResponse::Respond(200, $msg);
		}
	}
	
	public function setErrorHandler($h) {$this->http_err_handler = $h;}
	
	public function addListener($l) {
		$this->http_listeners[] = $l;
	}
}
?>